package com.glsc.ngateway.platform.controller.itflow;

import com.glsc.ngateway.common.api.common.enums.DictConstant;
import com.glsc.ngateway.common.api.common.enums.PageConstant;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.ProcessEditConfig;
import com.glsc.ngateway.common.base.domainmapper.ProcessEditConfigMapper;
import com.glsc.ngateway.common.api.flowable.dto.ProcessEditConfigSearchDto;
import com.glsc.ngateway.common.api.flowable.dto.task.config.ProcessEditConfigDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.platform.service.itflow.config.ProcessEditConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhengyr
 * @date 2023/12/7-15:20
 */
@RestController
@RequestMapping(value = "/it/flow/processEditConfig")
@Api(value = "流程节点参数配置", tags = {"流程节点参数配置"})
public class ProcessEditConfigController {
    @Resource
    private ProcessEditConfigService processEditConfigService;

    @ApiOperation(value = "查询配置", notes = "流程配置")
    @PostMapping("/search")
    public PlatformResponse<Page<ProcessEditConfig>> query(@RequestBody ProcessEditConfigSearchDto queryParam) throws Exception {
        if (queryParam.getPageNo() == null) {
            queryParam.setPageNo(PageConstant.PageNo);
        }
        if (queryParam.getPageSize() == null) {
            queryParam.setPageSize(PageConstant.PageSize);
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("EQ_processType", queryParam.getProcessType());
        paramMap.put("EQ_nodeType", queryParam.getNodeType());
        paramMap.put("EQ_activated", DictConstant.YES);
        paramMap.put("LIKE_nodeName", queryParam.getNodeName());
        return PlatformResponse.successData(processEditConfigService.findPage(queryParam.getPageNo(),queryParam.getPageSize(),paramMap));
    }

    @ApiOperation(value = "保存流程节点可编辑字段表设置信息", notes = "保存流程节点可编辑字段表设置信息")
    @PostMapping("/addOrUpdate")
    public PlatformResponse<Void> addOrUpdate(@RequestBody ProcessEditConfigDto processEditConfigDto) throws Exception {
        ProcessEditConfig processEditConfig = ProcessEditConfigMapper.INSTANCE.dto2domain(processEditConfigDto);
        processEditConfigService.saveOrUpdate(processEditConfig);
        return PlatformResponse.successMsg("操作成功");
    }

    @ApiOperation(value = "删除", notes = "删除")
    @RequestMapping(method = RequestMethod.POST, value = "delete")
    public PlatformResponse<Void> delete(@RequestBody ProcessEditConfigDto processEditConfigDto) throws Exception {
        processEditConfigService.updateDeletedByProcessTypeAndNodeType(processEditConfigDto.getProcessType(), processEditConfigDto.getNodeType());
        return PlatformResponse.successMsg("删除成功");
    }
}
